package com.weacar.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.weacar.R;

/**
 * Created by lizhaoyong on 2019/9/27.
 */

public class MySideBar extends View {

    public String[] index = {"#",
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    };

    private Paint paint = new Paint();

    /**
     * 索引字体颜色
     */
    private int mLetterColor;
    /**
     * 索引字体大小
     */
    private int mLetterSize;

    /**
     * 中间显示文字
     */
    private TextView mTextDialog;

    /**
     * 触摸事件接口
     */
    private OnTouchListener onTouchListener;

    public MySideBar(Context context) {
        super(context);
    }

    public MySideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public MySideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(context, attrs);
    }

    /**
     * 初始化属性
     *
     * @param context 上下文
     * @param attrs   属性
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SideBar);
        mLetterColor = array.getColor(R.styleable.SideBar_letterColor, Color.alpha(R.color.mainOrg));
        mLetterSize = array.getDimensionPixelSize(R.styleable.SideBar_letterSize, 30);
        array.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int singleHeight = getMeasuredHeight() / index.length;
        for (int i = 0; i < index.length; i++) {
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);   //  抗锯齿
            paint.setColor(mLetterColor);
            paint.setTextSize(mLetterSize);
            float xPos = getWidth() / 2 - paint.measureText(index[i]) / 2;
            float yPos = singleHeight * i + singleHeight * 0.6f;
            canvas.drawText(index[i], xPos, yPos, paint);
            paint.reset();// 重置画笔
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        final int c = (int) (y / getHeight() * index.length);
        switch (action) {
            case MotionEvent.ACTION_DOWN:  //  摁下
            case MotionEvent.ACTION_MOVE:  //  移动
                // setBackgroundColor(Color.parseColor("#40000000"));  //  遮罩层
                if (c >= 0 && c < index.length) {
                    if (onTouchListener != null) {
                        onTouchListener.onTouching(index[c].charAt(0));  //  触摸事件接口
                    }
                    if (mTextDialog != null) {  //  显示字母文本提示框
                        mTextDialog.setText(index[c]);
                        mTextDialog.setVisibility(View.VISIBLE);
                    }
                    invalidate();   //请求重新draw()
                }
                break;
            case MotionEvent.ACTION_UP:  //  抬起
                setBackgroundColor(Color.TRANSPARENT);
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }
        return true;
    }

    /**
     * 绑定LetterDialog
     *
     * @param mTextDialog 文本控件
     */
    public void setTextView(TextView mTextDialog) {
        this.mTextDialog = mTextDialog;
    }

    /**
     * 绑定触摸事件接口
     *
     * @param onTouchListener 触摸事件接口
     */
    public void setOnTouchListener(OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }

    /**
     * 触摸事件
     */
    public interface OnTouchListener {
        void onTouching(char c);
    }

}
